﻿//------------------------------------------------------------------------------
// <pcdd-entitiy>
//     彩票主流业务类库
//     彩种房间业务对象
// </pcdd-entitiy>
//------------------------------------------------------------------------------ 

using CL.Entity.Json.WebAPI;
using CL.Enum.Common;
using CL.Game.DAL;
using CL.Game.Entity;
using CL.Redis.BLL;
using CL.Tools.Common;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CL.Game.BLL
{
    /// <summary>
    /// LotteryRoomBLL 业务
    /// </summary>
    public class LotteryRoomBLL
    {
        LotteryRoomDAL dal = new LotteryRoomDAL(DbConnectionEnum.CaileGame);
        /// <summary>
        /// 插入对象
        /// </summary>
        /// <param name="Entity"></param>
        /// <returns></returns>
        public long InsertEntity(LotteryRoomEntity Entity)
        {
            return dal.Insert_Long(Entity) ?? 0;
        }
        /// <summary>
        /// 更新对象
        /// </summary>
        /// <param name="Entity"></param>
        /// <returns></returns>
        public long ModifyEntity(LotteryRoomEntity Entity)
        {
            return dal.Update(Entity);
        }
        /// <summary>
        /// 查询对象
        /// </summary>
        /// <param name="RoomID"></param>
        /// <returns></returns>
        public LotteryRoomEntity QueryEentity(long RoomID)
        {
            return dal.Get(RoomID);
        }
        /// <summary>
        /// 查询对象集
        /// </summary>
        /// <returns></returns>
        public List<LotteryRoomEntity> QueryEntitys()
        {
            return dal.GetList(new { IsEnable = true }, "RoomID desc").ToList();
        }

        /// <summary>
        /// 查询对象集
        /// </summary>
        /// <param name="LotteryCode"></param>
        /// <returns></returns>
        public List<LotteryRoomEntity> QueryEntitys(int LotteryCode)
        {
            return dal.GetList(new { LotteryCode = LotteryCode, IsEnable = true }, "RoomID DESC").ToList();
        }
        /// <summary>
        /// 根据房间名称查询
        /// </summary>
        /// <param name="LotteryCode">彩种编号</param>
        /// <param name="RoomCode">房间编号</param>
        /// <param name="RoomName">房间名称</param>
        /// <param name="PageIndex">当前页</param>
        /// <param name="PageSizem">页大小</param>
        /// <param name="RecordCount">总记录</param>
        /// <returns></returns>
        public List<LotteryRoomEntity> QueryListByLotteryCode(int LotteryCode, string RoomCode, string RoomName, int PageIndex, int PageSizem, ref int RecordCount)
        {
            var paras = new DynamicParameters();

            StringBuilder strSql = new StringBuilder();
            strSql.Append(" LotteryCode=@LotteryCode ");
            paras.Add("@LotteryCode", LotteryCode);
            if (!string.IsNullOrEmpty(RoomCode))
            {
                strSql.Append(" and RoomCode=@RoomCode ");
                paras.Add("@RoomCode", LotteryCode);
            }
            if (!string.IsNullOrEmpty(RoomName))
            {
                Func<string, string> encodeForLike = result => result.Replace("[", "[[]").Replace("%", "[%]");
                string strLike = "%" + encodeForLike(RoomName) + "%";
                strSql.Append(" and RoomName like @RoomName ");
                paras.Add("@RoomName", strLike);
            }
            RecordCount = dal.GetIntSingle(string.Format("select count(1) from CT_LotteryRoom where {0}", strSql), paras);
            return dal.GetListPaged(PageIndex, PageSizem, strSql.ToString(), "RoomID DESC ", paras).ToList();
        }

        #region 自定义方法
        /// <summary>
        /// 查询对应彩种房间
        /// </summary>
        /// <param name="LotteryCode"></param>
        /// <returns></returns>
        public LotteryRoomResult QueryRoomListByLotteryCode(int LotteryCode)
        {
            List<Room> room = null;
            List<Manager> manager = null;
            List<Rebate> rebate = null;

            BusinessRedis Redis = new BusinessRedis();
            RoomRebateBLL RebateBLL = new RoomRebateBLL();
            RoomManagerBLL ManagerBLL = new RoomManagerBLL();
            int RecCode = (int)ResultCode.Success;
            try
            {
                room = Redis.QueryRooms(LotteryCode);
                if (room == null || room.Count == 0)
                {
                    var LotteryRooms = this.QueryEntitys(LotteryCode);
                    if (LotteryRooms != null && LotteryRooms.Count > 0)
                    {
                        room = new List<Room>();
                        LotteryRooms.ForEach((item) =>
                        {
                            manager = ManagerBLL.QueryListByRoomID(item.RoomID).Select(s => new Manager()
                            {
                                ManagerID = s.ManagerID,
                                UserCode = s.UserCode,
                                UserNick = s.UserNike
                            }).ToList();
                            rebate = RebateBLL.QueryListByRoomID(item.RoomID).Select(s => new Rebate()
                            {
                                MaxAmount = s.MaxAmount,
                                MinAmount = s.MinAmount,
                                RebateID = s.RebateID,
                                Rebates = string.Format("{0}%", ((s.Rebates / 10000) * 100))
                            }).ToList();
                            room.Add(new Room()
                            {
                                ChatSet = item.ChatSet,
                                Frequency = item.Frequency,
                                RoomAuthor = item.RoomAuthor,
                                RoomCode = item.RoomCode,
                                RoomID = item.RoomID,
                                RoomName = item.RoomName,
                                RoomNotice = item.RoomNotice,
                                RoomNums = item.RoomNums,
                                ManagerData = manager,
                                RebateData = rebate
                            });
                        });
                        Redis.SaveRooms(LotteryCode, room);
                    }
                    else
                        RecCode = (int)ResultCode.NullData;
                }
            }
            catch (Exception ex)
            {
                new Log("LotteryRoomBLL").Write("查询对应彩种房间错误[CurrentIsuseInfo]：" + ex.StackTrace, true);
                RecCode = (int)ResultCode.SystemBusy;

            }
            return new LotteryRoomResult()
            {
                Code = RecCode,
                Msg = Common.GetDescription((ResultCode)RecCode),
                Data = RecCode != (int)ResultCode.Success ? null : room
            };
        }
        #endregion
    }
}
